package array;

// 有序数组的平方
public class leetcode977 {
    public static void main(String[] args) {
        int[] nums = {-4,-1,0,3,10};
        int[] sqrt = sortedSquares(nums);
        for (int i = 0; i < sqrt.length; i++) {
            System.out.print(sqrt[i] + " ");
        }
    }

    public static int[] sortedSquares(int[] nums){
        int[] sqrt = new int[nums.length];
        int left = 0;
        int flag = nums.length - 1;
        int right = nums.length - 1;
        while(left <= right){
            if(nums[left] * nums[left] >= nums[right] * nums[right]){
                sqrt[flag] = nums[left] * nums[left];
                left++;
            }
            else{
                sqrt[flag] = nums[right] * nums[right];
                right--;
            }
            flag--;
        }
        return sqrt;
    }
}